﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SilverTile.Xaml;
using System.Windows.Markup;
using SilverTile;

namespace SilverTileTests
{
    [TestClass]
    public class SilvertileGameTests
    {
        [TestMethod]
        public void SilverTileGame_Should_Be_An_IUpdateable()
        {

            var silverTileGame = new SilverTileGame();
            Assert.IsInstanceOfType(silverTileGame, typeof(IUpdateable), "SilverTileGame is not an IUpdateable");

        }

        [TestMethod]
        public void SilverTileGame_Should_Have_Properties_For_Width_And_Height()
        {

            var silverTileGame = new SilverTileGame();

            silverTileGame.Width = 100;
            silverTileGame.Height = 150;

            Assert.AreEqual(100, silverTileGame.Width, "Width was not set correctly");
            Assert.AreEqual(silverTileGame.Width, silverTileGame.GetValue(SilverTileGame.WidthProperty), "Width and WidthProperty does not return the same values");
            Assert.AreEqual(150, silverTileGame.Height, "Height was not set correctly");
            Assert.AreEqual(silverTileGame.Height, silverTileGame.GetValue(SilverTileGame.HeightProperty), "Height and HeightProperty does not return the same values");

        }

        [TestMethod]
        public void SilverTileGame_Should_Expose_Viewport_With_Same_Dimensions()
        {
            var silverTileGame = new SilverTileGame();

            silverTileGame.Width = 100;
            silverTileGame.Height = 150;

            Assert.AreEqual(silverTileGame.Width, silverTileGame.Viewport.Width, "Viewport width is not equal to Width");
            Assert.AreEqual(silverTileGame.Height, silverTileGame.Viewport.Height, "Viewport height is not equal to Height");

        }

        [TestMethod]
        public void SilverTileGame_Should_Create_Viewport_When_Both_Width_And_Height_Are_Set()
        {
            var silverTileGame = new SilverTileGame();

            Assert.IsNull(silverTileGame.Viewport, "Viewport is not null before width and height are set");

            silverTileGame.Width = 100;
            Assert.IsNull(silverTileGame.Viewport, "Viewport is not null before height is set");

            silverTileGame.Height = 150;
            Assert.IsNotNull(silverTileGame.Viewport, "Viewport is null after width and height are set");

        }

        [TestMethod]
        public void SilverTileGame_Should_Have_GameWorld_Property_And_Active_GameArea()
        {

            string gameWorldXaml = @"
                <ResourceDictionary
                    xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" 
                    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
                    xmlns:st=""clr-namespace:SilverTile.Xaml;assembly=SilverTile"">

                    <st:SpriteFrame x:Key=""Sand"" Source=""/SilverTileTests;component/Tiles/0002.png"" />    

                    <st:GameAreaGrid x:Key=""Area1"" AreaName=""Area1"" Columns=""2"" Rows=""2"" TileWidth=""32"" TileHeight=""32"">
                        <st:SpriteTile X=""0"" Y=""0"" Blocking=""False"" Frame=""{StaticResource Sand}"" />
                        <st:SpriteTile X=""1"" Y=""0"" Blocking=""False"" Frame=""{StaticResource Sand}"" />
                        <st:SpriteTile X=""0"" Y=""1"" Blocking=""False"" Frame=""{StaticResource Sand}"" />
                        <st:SpriteTile X=""1"" Y=""1"" Blocking=""False"" Frame=""{StaticResource Sand}"" />
                    </st:GameAreaGrid>

                    <st:GameAreaGrid x:Key=""Area2"" AreaName=""Area2"" Columns=""2"" Rows=""2"" TileWidth=""32"" TileHeight=""32"">
                        <st:SpriteTile X=""0"" Y=""0"" Blocking=""False"" Frame=""{StaticResource Sand}"" />
                        <st:SpriteTile X=""1"" Y=""0"" Blocking=""False"" Frame=""{StaticResource Sand}"" />
                        <st:SpriteTile X=""0"" Y=""1"" Blocking=""False"" Frame=""{StaticResource Sand}"" />
                        <st:SpriteTile X=""1"" Y=""1"" Blocking=""False"" Frame=""{StaticResource Sand}"" />
                    </st:GameAreaGrid>

                    <st:SilverTileGameWorld x:Key=""GameWorld"" StartAreaName=""Area1"">
                        <st:SilverTileGameWorld.GameAreas>
                            <StaticResource ResourceKey=""Area1"" />
                            <StaticResource ResourceKey=""Area2"" />
                        </st:SilverTileGameWorld.GameAreas>
                    </st:SilverTileGameWorld>
                </ResourceDictionary>
            ";
            
            var silverTileGame = new SilverTileGame();

            ResourceDictionary dict = (ResourceDictionary)XamlReader.Load(gameWorldXaml);
            var gameWorld = (SilverTileGameWorld)dict["GameWorld"];

            silverTileGame.GameWorld = gameWorld;

            Assert.AreSame(gameWorld, silverTileGame.GameWorld, "GameWorld was not set properly");
            Assert.AreSame(silverTileGame.ActiveGameArea, dict["Area1"], "ActiveGameArea was not set properly");
            
        }

        [TestMethod]
        public void GameWorld_Assigned_To_Game_Should_Get_Its_Viewport_Set()
        {

            var gameWorld = new SilverTileGameWorld();
            var game = new SilverTileGame { Width = 10, Height = 10 };

            Assert.IsNull(gameWorld.Viewport, "GameWorlds Viewport is not null before it is assigned to Game");
            game.GameWorld = gameWorld;
            Assert.IsNotNull(gameWorld.Viewport, "GameWorlds Viewport is null after it is assigned to Game");

        }


    }
}